package com.itfreer.data.config;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.data.config.dao.ConfigVersionDao;
import com.itfreer.data.config.entity.ConfigVersionEntity;
import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.BaseServiceImp;
import com.itfreer.form.api.PageInfo;

/**
 * 配置版本
 */
@Component
public class ConfigVersionServiceImp extends BaseServiceImp<ConfigVersionEntity> implements ConfigVersionService {

	@Autowired
	private ConfigVersionDao dao;

	@Override
	protected BaseDao<ConfigVersionEntity> getDao() {
		return dao;
	}

	/**
	 * 获取版本
	 * 
	 * @return
	 */
	public Integer getVersion() {
		PageInfo pInfo = new PageInfo();
		pInfo.setPageIndex(1);
		pInfo.setPageSize(1);
		List<ConfigVersionEntity> entitys = this.getEntitys(pInfo);
		if (entitys == null || entitys.size() <= 0) {
			return 0;
		} else {
			return entitys.get(0).getVersion();
		}
	}

	/**
	 * 版本号加1
	 */
	public void addVersion() {
		PageInfo pInfo = new PageInfo();
		pInfo.setPageIndex(1);
		pInfo.setPageSize(1);
		List<ConfigVersionEntity> entitys = this.getEntitys(pInfo);
		if (entitys == null || entitys.size() <= 0) {
			ConfigVersionEntity entity = new ConfigVersionEntity();
			entity.setId(UUID.randomUUID().toString().replace("-", ""));
			entity.setVersion(0);
			this.add(entity);
		} else {
			ConfigVersionEntity entity = entitys.get(0);
			entity.setVersion(entity.getVersion() + 1);
			this.update(entity);
		}
	}
}
